package com.jqk.consumer.listener;

import com.jqk.consumer.entity.Product;
import com.jqk.consumer.repository.ProductRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:JiangQiKun
 * @Date:2022/03/21 12:58 AM
 * Description:
 * com.jqk.consumer.listenerConsumerListener
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "order",consumerGroup = "order-group")
@RequiredArgsConstructor(onConstructor = @__(@Autowired) )
public class ConsumerListener implements RocketMQListener<Product> {

  @Autowired
  private ProductRepository productRepository;

    @Override
    public void onMessage(Product product) {
        log.info("received message: {}",product);
        Product one = productRepository.query().eq("product_code", product.getProductCode()).one();
        one.setAmount(one.getAmount()-product.getAmount());
        productRepository.saveOrUpdate(one);
        log.info("dcl product success");
    }
}
